package org.itboys.core.service.mongo.front;

import org.apache.commons.lang.StringUtils;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.ExceptionEnum;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.order.SundaySendOrderMapper;
import org.itboys.core.dto.invoice.SundayInvoiceDTO;
import org.itboys.core.dto.invoice.SundayInvoiceDetailDTO;
import org.itboys.core.dto.invoice.SundayInvoiceOrderDTO;
import org.itboys.core.entity.mongo.SundayInvoice;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.entity.mysql.order.SundaySendOrder;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.core.util.PhoneUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Author：贾琦磊
 * @Date：2018/11/27
 * @Description：发票
 */
@Service
public class SundayFrontInvoiceService extends BaseMongoService<SundayInvoice> {
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayInvoice> getEntityClass() {
        return SundayInvoice.class;
    }

    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundaySendOrderMapper sundaySendOrderMapper;

    public void save(List<Long> orderIds, Long memberId, Integer type, Integer headType, String head, String dutyNo,
                     String money, String email, String name, String mobile, String area, String address) {
        if (StringUtils.isNotEmpty(mobile)) {
            if (!PhoneUtil.isMobileNumber(mobile))
                throw new ErrorException(ExceptionEnum.MOBILE_FORMAT_ERROR.getValue());
        }
        SundayMember member = memberService.getById(memberId);
        SundayInvoice invoice = new SundayInvoice(memberId, member.getRealName(), type, headType, head,
                dutyNo, money, email, name,
                mobile, area, address, SundayCommonConstants.INVOICE_STATUS_INIT);

        super.save(invoice);
        //将订单状态改为已开具发票
        Map<String, Object> param = new HashMap<>();
        param.put("invoiced", SundayCommonConstants.YES);
        sundaySendOrderMapper.batchUpdateFields(param, orderIds);
    }

    /**
     * 查询开具发票
     *
     * @param request
     * @param memberId
     * @return
     */
    public List<SundayInvoiceDTO> select(HttpServletRequest request, Long memberId) {
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request);
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId", memberId);
        PageResult<SundayInvoice> result = super.pageQuery(request, param);
        List<SundayInvoice> data = result.getData();
        if (!ListUtil.isNotNull(data)) {
            return new ArrayList<>();
        }
        List<SundayInvoiceDTO> invoiceDTOS = new ArrayList<>();
        for (SundayInvoice i : data
                ) {
            SundayInvoiceDTO dto = new SundayInvoiceDTO();
            BeanUtils.copyProperties(i, dto);
            invoiceDTOS.add(dto);
        }
        return invoiceDTOS;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    public SundayInvoiceDetailDTO detail(Long id) {
        SundayInvoice invoice = super.getById(id);
        SundayInvoiceDetailDTO detailDTO = new SundayInvoiceDetailDTO();
        BeanUtils.copyProperties(invoice, detailDTO);
        return detailDTO;
    }

    /**
     * 获取可以开发票的订单，状态为已完成或一买断且未开具发票的订单
     *
     * @param request
     * @return
     */
    public List<SundayInvoiceOrderDTO> getOrders(HttpServletRequest request, Long memberId) {
        List<SundayInvoiceOrderDTO> result = new ArrayList<>();
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request);
        param.put("invoiced", SundayCommonConstants.NO);
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId", memberId);
        Integer[] status = {SundayCommonConstants.send_order_status_ymd, SundayCommonConstants.send_order_status_ywc};
        param.put("statuses", Arrays.asList(status));
        List<SundaySendOrder> data = sundaySendOrderMapper.select(param);
        if (ListUtil.isNull(data)) return result;
        for (SundaySendOrder so : data
                ) {
            SundayInvoiceOrderDTO dto = new SundayInvoiceOrderDTO();
            dto.setMoney(so.getRentAmount());
            dto.setOrderId(so.getId());
            dto.setDate(so.getCreateDate());
            dto.setOrderNo(so.getOrderNo());
            result.add(dto);
        }
        return result;
    }
}
